package p.ithorns.integration.hazelcast.mqtt;


import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.PooledByteBufAllocator;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * DataHandlerContext
 * <p>
 * 消息处理器上下文
 *
 * @author liuhaitao
 * @version 1.0
 * @date 2022/3/15 11:52
 */
public class DataHandlerContext {

    private final static ByteBufAllocator BYTE_BUF_ALLOCATOR = PooledByteBufAllocator.DEFAULT;

    private static final Map<Serializable, DataHandler> HANDLER_MAP = new HashMap<>();

    public static void register(Serializable code, DataHandler handler) {
        HANDLER_MAP.putIfAbsent(code, handler);
    }

    /**
     * 根据指令码获取处理器
     *
     * @param code -指令
     * @param <E>  -消息类型
     * @return DataHandler
     */
    public static <E> DataHandler<E> get(Serializable code) {
        return HANDLER_MAP.get(code);
    }

    public static ByteBufAllocator getAllocator() {
        return BYTE_BUF_ALLOCATOR;
    }

}